package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final  String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        // return nursingLevelMapper.insert(nursingLevel);
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int flag = nursingLevelMapper.insert(nursingLevel);
        //删除缓存
        deleteCache();
        return flag;
    }

    private void deleteCache() {
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }


    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        // return nursingLevelMapper.updateById(nursingLevel);
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int flag = nursingLevelMapper.updateById(nursingLevel);
        deleteCache();
        return flag;
    }


    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        // return nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        int flag = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        deleteCache();
        return flag;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        // return nursingLevelMapper.deleteById(id);
        int flag = nursingLevelMapper.deleteById(id);
        deleteCache();
        return flag;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public List<NursingLevel> getAll() {

        //从缓存中获取
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        //缓存中有数据，直接返回
        if (!CollectionUtil.isEmpty(list)) {
            return list;
        }
        //缓存中没有数据，从数据库中查询
        list = lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        //将数据写入缓存中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        //添加缓存过期时间 5分钟
        redisTemplate.expire(CACHE_KEY_PREFIX, 5, TimeUnit.MINUTES);
        return list;
    }
}
